<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" />
        <span>黑马电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边栏菜单 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="changeCollapse">|||</div>
        <el-menu
          background-color="#363c42"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
          :default-active="aPath"
        >
          <!-- 一级菜单  循环一级菜单 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单  循环一级菜单children列表-->
            <el-menu-item
              :index="'/' + sonItem.path"
              v-for="sonItem in item.children"
              :key="sonItem.id"
              @click="chooseActivePath(sonItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ sonItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created: function () {
    // 菜单列表数据
    this.getMenuList()
    // 从本地读取path 设置高亮
    this.aPath = window.sessionStorage.getItem('apath')
  },
  data () {
    return {
      // 菜单数组
      menuList: [],
      // 一级菜单字体图标样式 id值加属性
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单是否折叠动态属性
      isCollapse: false,
      // 点击菜单 高亮
      aPath: ''
    }
  },
  methods: {
    // 1.退出操作
    logout () {
      this.$messageBox.confirm('尊敬的VIP用户，您是否确定退出?', '系统提示', {
        confirmButtonText: '确定',
        callback: (action) => {
          if (action === 'confirm') {
            window.sessionStorage.clear()
            this.$router.push('/login') // 编程式导航
          }
        }
      })
    },
    // 2.获取左侧菜单栏列表
    // 2.1 接口获取数据
    // 2.2 在标签内通过循环两级菜单生成结构
    async getMenuList () {
      const { data: resData } = await this.$http.get('menus')
      if (resData.meta.status !== 200) { return this.$message.error(resData.meta.msg) }
      this.menuList = resData.data
      console.log(resData)
    },
    // 3.点击切换true/false
    changeCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 点击左侧菜单栏 高亮
    chooseActivePath (aPath) {
      window.sessionStorage.setItem('apath', aPath)
      this.aPath = aPath
    }
  }
}
</script>

<style lang="less" scoped>
// element标签属于类名
.el-header {
  background-color: #363c42;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333c42;
  border-right: none;
}
.el-main {
  background-color: #e9edf1;
}
.home-container {
  height: 100%;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
